<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="pageParam.model.bedId"
        style="width: 200px;"
        placeholder="姓名查询..."
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @input="handleFilter"
      />
      <!-- <el-select style="width: 140;" v-model="pageParam.orderParams" class="filter-item" placeholder="创建时间">
        <el-option v-for="item in createTimeOrderOptions" :key="item.key" :label="item.label" :value="item.value" />
      </el-select> -->
      <el-select v-model="pageParam.model.nursingCategoryId" placeholder="请选择护理类别" @change="getListByPage()">
        <el-option label="全部" value="" />
        <el-option v-for="item in nursingList" :key="item.id" :label="item.nursingName" :value="item.id" />
      </el-select>
      <el-select v-model="pageParam.model.nursingStatus" placeholder="请选择护理类别" @change="getListByPage()">
        <el-option label="全部" value="" />
        <el-option label="未执行" value="0" />
        <el-option label="已执行" value="1" />
      </el-select>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table :data="list" fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
      <el-table-column prop="patientName" label="病人姓名" />
      <el-table-column prop="nursingName" label="护理项目名" />
      <el-table-column prop="nurseName" label="护士名" />
      <el-table-column prop="nursingStatus" label="护理状态">
        <template slot-scope="scope">
          <span v-if="scope.row.nursingStatus==0">待处理</span>
          <span v-if="scope.row.nursingStatus==1">已完成</span>
        </template>
      </el-table-column>
      <el-table-column prop="patientStatus" label="病人状态" />
      <el-table-column sortable="custom" prop="nursingTime" label="护理日期" />

      <el-table-column label="操作" fit>
        <template slot-scope="{row,$index}">
          <el-button size="small" type="warning" v-if="row.nursingStatus=='0'" @click="handleUpdate(row)">编辑</el-button>
          <el-button size="small" type="info" v-else >已完成</el-button>
          <el-button size="small" type="danger" v-permission="['admin']">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getListByPage"
    />
    <!-- 弹窗组件 -->
    <el-dialog title="添加护理" :visible.sync="dialogFormVisible">
      <el-form
        :model="temp"
        label-position="right"
        label-width="140px"
        style="width: 400px; margin-left:50px;"
      >

        <el-form-item label="输入病人住院编号" prop="time">
          <el-input v-model="no" />
          <el-button @click="getPatientByNo">
            查询
          </el-button>
          <span>{{ application == null ? "" : application.patientName }}</span>
        </el-form-item>

        <el-form-item label="护理项目" prop="userName">
          <el-select v-model="temp.nursingCategoryId" placeholder="请选择护理护理项目">
            <el-option v-for="item in nursingList" :key="item.id" :label="item.nursingName" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="护理执行信息填写">
      <el-form ref="dataForm" :model="status" label-position="right" label-width="80px">
        <!--               style="width: 400px; margin-left:50px;">-->
        <el-form-item label="病人状态">
          <el-input v-model="status.patientStatus" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitNurse">完成护理</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getByNo } from '@/api/application/application'
import {
  getNursingCategory,
  getAllNursing,
  add,
  update
} from '@/api/ward/nursing'
// 导入组件  记得声明组件
import Pagination from '@/components/Pagination'

export default {
  components: { // 在这里声明组件
    Pagination
  },
  data() {
    return {
      no: '',
      application: [],
      patient: [],
      list: [], // 列表数据
      total: 0,
      listQuery: { // 分页 排序  查询参数
        pageNum: 1,
        pageSize: 5
      },
      fieldQuery: { // 按字段查询
        realName: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dialogPvVisible: false,
      temp: {
        patientId: undefined,
        nursingCategoryId: ''
      },
      status: {
      },
      pageParam: {
        pageNum: 1,
        pageSize: 5,
        model: {
          bedId: '',
          nursingCategoryId: undefined,
          nursingStatus: ''
        },
        orderParams: [],
        superSearchKeyWord: ''
      },
      nursingList: [],
      nurseList: []
    }
  },
  created() {
    this.getListByPage()
    // 拿到所有的护理类别
    getNursingCategory().then(res => {
      this.nursingList = res.content
    })
  },
  methods: {
    // 根据病人住院编号获取病人信息
    getPatientByNo() {
      getByNo(this.no).then(res => {
        this.temp.patientId=''
        this.application = res.content
        this.nurseList = []
        this.temp.nurseId = ''
        // 获取该科室下面的护士
        if (this.application == null) {
          return
        }
        this.temp.patientId = this.application.patientId
      })
    },
    handleUpdate(row) {
      this.dialogPvVisible = true
      this.status = row
    },
    submitNurse() {
      // 添加病人状态,修改护理记录为以完成
      update(this.status).then(res => {
        this.$notify({
          title: 'Success',
          message: 'Created Successfully',
          type: 'success',
          duration: 2000
        })
        this.getListByPage()
        this.dialogPvVisible = false
      })
    },
    sortChange(val) {
      // console.log("排序参数",val)
      const prop = val.prop
      const order = val.order === 'ascending' ? 'asc' : 'desc'
      let orderParam
      orderParam = 'nursing_time' + ' ' + order
      this.pageParam.orderParams = []
      this.pageParam.orderParams.push(orderParam)
      this.getListByPage()
    },
    getListByPage() {
      this.pageParam.pageNum = this.listQuery.pageNum
      this.pageParam.pageSize = this.listQuery.pageSize
      getAllNursing(this.pageParam).then(res => {
        // console.log(res)
        this.list = res.content.list
        this.total = res.content.total
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getListByPage()
    },
    handleCreate() {
      this.dialogFormVisible = true
    },
    createData() {
      add(this.temp).then(() => {
        this.dialogFormVisible = false
        this.$notify({
          title: 'Success',
          message: 'Created Successfully',
          type: 'success',
          duration: 2000
        })
        this.getListByPage()
      })
    }
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
